<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li class="active"><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 模块化">1. 模块化</a><ul><li><a href="#t11.1 命名空间">1.1 命名空间</a></li><li><a href="#t21.2 CommonJS">1.2 CommonJS</a><ul><li><a href="#t31.2.1 用法">1.2.1 用法</a></li><li><a href="#t41.2.2 原理">1.2.2 原理</a></li></ul></li><li><a href="#t51.3 AMD">1.3 AMD</a><ul><li><a href="#t61.3.1 用法">1.3.1 用法</a></li><li><a href="#t71.3.2 原理">1.3.2 原理</a></li></ul></li><li><a href="#t81.4 ES6 模块化">1.4 ES6 模块化</a></li></ul></li><li><a href="#t92. 自动化构建">2. 自动化构建</a></li><li><a href="#t103. Webpack">3. Webpack</a></li><li><a href="#t113.1 安装 Webpack">3.1 安装 Webpack</a></li><li><a href="#t123.1.1  安装 Webpack 到本项目">3.1.1  安装 Webpack 到本项目</a></li><li><a href="#t133.1.2  安装 Webpack 到全局">3.1.2  安装 Webpack 到全局</a></li><li><a href="#t143.1.2 使用 Webpack">3.1.2 使用 Webpack</a></li><li><a href="#t153.1.3 使用 Webpack">3.1.3 使用 Webpack</a></li><li><a href="#t163.1.4 依赖其它模块">3.1.4 依赖其它模块</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <p>&#xFEFF;</p>
<h2 id="t01. &#x6A21;&#x5757;&#x5316;">1. &#x6A21;&#x5757;&#x5316; <a href="#t01. &#x6A21;&#x5757;&#x5316;"> # </a></h2>
<p>&#x6A21;&#x5757;&#x5316;&#x662F;&#x6307;&#x628A;&#x4E00;&#x4E2A;&#x590D;&#x6742;&#x7684;&#x7CFB;&#x7EDF;&#x5206;&#x89E3;&#x5230;&#x591A;&#x4E2A;&#x6A21;&#x5757;&#x4EE5;&#x65B9;&#x4FBF;&#x7F16;&#x7801;&#x3002;</p>
<h3 id="t11.1 &#x547D;&#x540D;&#x7A7A;&#x95F4;">1.1 &#x547D;&#x540D;&#x7A7A;&#x95F4; <a href="#t11.1 &#x547D;&#x540D;&#x7A7A;&#x95F4;"> # </a></h3>
<p>&#x5F00;&#x53D1;&#x7F51;&#x9875;&#x8981;&#x901A;&#x8FC7;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x7EC4;&#x7EC7;&#x4EE3;&#x7801;</p>
<pre><code>&lt;script src=&quot;jquery.js&quot;&gt;
</code></pre><ul>
<li>&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x51B2;&#x7A81;&#xFF0C;&#x4E24;&#x4E2A;&#x5E93;&#x53EF;&#x80FD;&#x4F1A;&#x4F7F;&#x7528;&#x540C;&#x4E00;&#x4E2A;&#x540D;&#x79F0;</li>
<li>&#x65E0;&#x6CD5;&#x5408;&#x7406;&#x5730;&#x7BA1;&#x7406;&#x9879;&#x76EE;&#x7684;&#x4F9D;&#x8D56;&#x548C;&#x7248;&#x672C;&#xFF1B;</li>
<li>&#x65E0;&#x6CD5;&#x65B9;&#x4FBF;&#x5730;&#x63A7;&#x5236;&#x4F9D;&#x8D56;&#x7684;&#x52A0;&#x8F7D;&#x987A;&#x5E8F;&#x3002;</li>
</ul>
<h3 id="t21.2 CommonJS">1.2 CommonJS <a href="#t21.2 CommonJS"> # </a></h3>
<p>CommonJS &#x662F;&#x4E00;&#x79CD;&#x4F7F;&#x7528;&#x5E7F;&#x6CDB;&#x7684;<code>JavaScript</code>&#x6A21;&#x5757;&#x5316;&#x89C4;&#x8303;&#xFF0C;&#x6838;&#x5FC3;&#x601D;&#x60F3;&#x662F;&#x901A;&#x8FC7;<code>require</code>&#x65B9;&#x6CD5;&#x6765;&#x540C;&#x6B65;&#x5730;&#x52A0;&#x8F7D;&#x4F9D;&#x8D56;&#x7684;&#x5176;&#x4ED6;&#x6A21;&#x5757;&#xFF0C;&#x901A;&#x8FC7; module.exports &#x5BFC;&#x51FA;&#x9700;&#x8981;&#x66B4;&#x9732;&#x7684;&#x63A5;&#x53E3;&#x3002;</p>
<h4 id="t31.2.1 &#x7528;&#x6CD5;">1.2.1 &#x7528;&#x6CD5; <a href="#t31.2.1 &#x7528;&#x6CD5;"> # </a></h4>
<p>&#x91C7;&#x7528; CommonJS &#x5BFC;&#x5165;&#x53CA;&#x5BFC;&#x51FA;&#x65F6;&#x7684;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>// &#x5BFC;&#x5165;
const someFun= require(&apos;./moduleA&apos;);
someFun();

// &#x5BFC;&#x51FA;
module.exports = someFunc;
</code></pre><h4 id="t41.2.2 &#x539F;&#x7406;">1.2.2 &#x539F;&#x7406; <a href="#t41.2.2 &#x539F;&#x7406;"> # </a></h4>
<p>a.js</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> b = req(<span class="hljs-string">&apos;./b.js&apos;</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">req</span>(<span class="hljs-params">mod</span>) </span>{
    <span class="hljs-keyword">let</span> filename = path.join(__dirname, mod);
    <span class="hljs-keyword">let</span> content = fs.readFileSync(filename, <span class="hljs-string">&apos;utf8&apos;</span>);
    <span class="hljs-keyword">let</span> fn = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Function</span>(<span class="hljs-string">&apos;exports&apos;</span>, <span class="hljs-string">&apos;require&apos;</span>, <span class="hljs-string">&apos;module&apos;</span>, <span class="hljs-string">&apos;__filename&apos;</span>, <span class="hljs-string">&apos;__dirname&apos;</span>, content + <span class="hljs-string">&apos;\n return module.exports;&apos;</span>);
    <span class="hljs-keyword">let</span> <span class="hljs-built_in">module</span> = {
        <span class="hljs-attr">exports</span>: {}
    };

    <span class="hljs-keyword">return</span> fn(<span class="hljs-built_in">module</span>.exports, req, <span class="hljs-built_in">module</span>, __filename, __dirname);
}
</code></pre>
<p>b.js</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;bbb&apos;</span>);
exports.name = <span class="hljs-string">&apos;zfpx&apos;</span>;
</code></pre>
<h3 id="t51.3 AMD">1.3 AMD <a href="#t51.3 AMD"> # </a></h3>
<p>AMD &#x4E5F;&#x662F;&#x4E00;&#x79CD; JavaScript &#x6A21;&#x5757;&#x5316;&#x89C4;&#x8303;&#xFF0C;&#x4E0E; CommonJS &#x6700;&#x5927;&#x7684;&#x4E0D;&#x540C;&#x5728;&#x4E8E;&#x5B83;&#x91C7;&#x7528;&#x5F02;&#x6B65;&#x7684;&#x65B9;&#x5F0F;&#x53BB;&#x52A0;&#x8F7D;&#x4F9D;&#x8D56;&#x7684;&#x6A21;&#x5757;&#x3002; AMD &#x89C4;&#x8303;&#x4E3B;&#x8981;&#x662F;&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x9488;&#x5BF9;&#x6D4F;&#x89C8;&#x5668;&#x73AF;&#x5883;&#x7684;&#x6A21;&#x5757;&#x5316;&#x95EE;&#x9898;&#xFF0C;&#x6700;&#x5177;&#x4EE3;&#x8868;&#x6027;&#x7684;&#x5B9E;&#x73B0;&#x662F; requirejs&#x3002;</p>
<p>AMD &#x7684;&#x4F18;&#x70B9;</p>
<ul>
<li>&#x53EF;&#x5728;&#x4E0D;&#x8F6C;&#x6362;&#x4EE3;&#x7801;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x76F4;&#x63A5;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x8FD0;&#x884C;</li>
<li>&#x53EF;&#x52A0;&#x8F7D;&#x591A;&#x4E2A;&#x4F9D;&#x8D56;</li>
<li>&#x4EE3;&#x7801;&#x53EF;&#x8FD0;&#x884C;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x73AF;&#x5883;&#x548C; Node.js &#x73AF;&#x5883;&#x4E0B;</li>
</ul>
<p>AMD &#x7684;&#x7F3A;&#x70B9;</p>
<ul>
<li>JavaScript &#x8FD0;&#x884C;&#x73AF;&#x5883;&#x6CA1;&#x6709;&#x539F;&#x751F;&#x652F;&#x6301; AMD&#xFF0C;&#x9700;&#x8981;&#x5148;&#x5BFC;&#x5165;&#x5B9E;&#x73B0;&#x4E86; AMD &#x7684;&#x5E93;&#x540E;&#x624D;&#x80FD;&#x6B63;&#x5E38;&#x4F7F;&#x7528;&#x3002;</li>
</ul>
<h4 id="t61.3.1 &#x7528;&#x6CD5;">1.3.1 &#x7528;&#x6CD5; <a href="#t61.3.1 &#x7528;&#x6CD5;"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6A21;&#x5757;</span>
define(<span class="hljs-string">&apos;a&apos;</span>, [], <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;a&apos;</span>;
});
define(<span class="hljs-string">&apos;b&apos;</span>, [<span class="hljs-string">&apos;a&apos;</span>], <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">a</span>) </span>{
    <span class="hljs-keyword">return</span> a + <span class="hljs-string">&apos;b&apos;</span>;
});
<span class="hljs-comment">// &#x5BFC;&#x5165;&#x548C;&#x4F7F;&#x7528;</span>
<span class="hljs-built_in">require</span>([<span class="hljs-string">&apos;b&apos;</span>], <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">b</span>) </span>{
    <span class="hljs-built_in">console</span>.log(b);
});
</code></pre>
<h4 id="t71.3.2 &#x539F;&#x7406;">1.3.2 &#x539F;&#x7406; <a href="#t71.3.2 &#x539F;&#x7406;"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> factories = {};
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">define</span>(<span class="hljs-params">modName, dependencies, factory</span>) </span>{
    factory.dependencies = dependencies;
    factories[modName] = factory;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">require</span>(<span class="hljs-params">modNames, callback</span>) </span>{
    <span class="hljs-keyword">let</span> loadedModNames = modNames.map(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">modName</span>) </span>{
        <span class="hljs-keyword">let</span> factory = factories[modName];
        <span class="hljs-keyword">let</span> dependencies = factory.dependencies;
        <span class="hljs-keyword">let</span> exports;
        <span class="hljs-built_in">require</span>(dependencies, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">...dependencyMods</span>) </span>{
            exports = factory.apply(<span class="hljs-literal">null</span>, dependencyMods);
        });
        <span class="hljs-keyword">return</span> exports;
    })
    callback.apply(<span class="hljs-literal">null</span>, loadedModNames);
}


</code></pre>
<h3 id="t81.4 ES6 &#x6A21;&#x5757;&#x5316;">1.4 ES6 &#x6A21;&#x5757;&#x5316; <a href="#t81.4 ES6 &#x6A21;&#x5757;&#x5316;"> # </a></h3>
<p>ES6 &#x6A21;&#x5757;&#x5316;&#x662F;<code>ECMA</code>&#x63D0;&#x51FA;&#x7684;<code>JavaScript</code>&#x6A21;&#x5757;&#x5316;&#x89C4;&#x8303;&#xFF0C;&#x5B83;&#x5728;&#x8BED;&#x8A00;&#x7684;&#x5C42;&#x9762;&#x4E0A;&#x5B9E;&#x73B0;&#x4E86;&#x6A21;&#x5757;&#x5316;&#x3002;&#x6D4F;&#x89C8;&#x5668;&#x5382;&#x5546;&#x548C;<code>Node.j</code>s <code>&#x90FD;&#x5BA3;&#x5E03;&#x8981;&#x539F;&#x751F;&#x652F;&#x6301;&#x8BE5;&#x89C4;&#x8303;&#x3002;&#x5B83;&#x5C06;&#x9010;&#x6E10;&#x53D6;&#x4EE3;</code>CommonJS<code>&#x548C;</code>AMD`&#x89C4;&#x8303;&#xFF0C;&#x6210;&#x4E3A;&#x6D4F;&#x89C8;&#x5668;&#x548C;&#x670D;&#x52A1;&#x5668;&#x901A;&#x7528;&#x7684;&#x6A21;&#x5757;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x3002;
&#x91C7;&#x7528; ES6 &#x6A21;&#x5757;&#x5316;&#x5BFC;&#x5165;&#x53CA;&#x5BFC;&#x51FA;&#x65F6;&#x7684;&#x4EE3;&#x7801;&#x5982;&#x4E0B;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x5BFC;&#x5165;</span>
<span class="hljs-keyword">import</span> { name } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./person.js&apos;</span>;
<span class="hljs-comment">// &#x5BFC;&#x51FA;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> name = <span class="hljs-string">&apos;zfpx&apos;</span>;
</code></pre>
<p>ES6&#x6A21;&#x5757;&#x867D;&#x7136;&#x662F;&#x7EC8;&#x6781;&#x6A21;&#x5757;&#x5316;&#x65B9;&#x6848;&#xFF0C;&#x4F46;&#x5B83;&#x7684;&#x7F3A;&#x70B9;&#x5728;&#x4E8E;&#x76EE;&#x524D;&#x65E0;&#x6CD5;&#x76F4;&#x63A5;&#x8FD0;&#x884C;&#x5728;&#x5927;&#x90E8;&#x5206; JavaScript &#x8FD0;&#x884C;&#x73AF;&#x5883;&#x4E0B;&#xFF0C;&#x5FC5;&#x987B;&#x901A;&#x8FC7;&#x5DE5;&#x5177;&#x8F6C;&#x6362;&#x6210;&#x6807;&#x51C6;&#x7684; ES5 &#x540E;&#x624D;&#x80FD;&#x6B63;&#x5E38;&#x8FD0;&#x884C;&#x3002;</p>
<h2 id="t92. &#x81EA;&#x52A8;&#x5316;&#x6784;&#x5EFA;">2. &#x81EA;&#x52A8;&#x5316;&#x6784;&#x5EFA; <a href="#t92. &#x81EA;&#x52A8;&#x5316;&#x6784;&#x5EFA;"> # </a></h2>
<p>&#x6784;&#x5EFA;&#x5C31;&#x662F;&#x505A;&#x8FD9;&#x4EF6;&#x4E8B;&#x60C5;&#xFF0C;&#x628A;&#x6E90;&#x4EE3;&#x7801;&#x8F6C;&#x6362;&#x6210;&#x53D1;&#x5E03;&#x5230;&#x7EBF;&#x4E0A;&#x7684;&#x53EF;&#x6267;&#x884C; JavaScrip&#x3001;CSS&#x3001;HTML &#x4EE3;&#x7801;&#xFF0C;&#x5305;&#x62EC;&#x5982;&#x4E0B;&#x5185;&#x5BB9;&#x3002;</p>
<ul>
<li>&#x4EE3;&#x7801;&#x8F6C;&#x6362;&#xFF1A;ECMASCRIPT6 &#x7F16;&#x8BD1;&#x6210; ECMASCRIPT5&#x3001;LESS &#x7F16;&#x8BD1;&#x6210; CSS &#x7B49;&#x3002;</li>
<li>&#x6587;&#x4EF6;&#x4F18;&#x5316;&#xFF1A;&#x538B;&#x7F29; JavaScript&#x3001;CSS&#x3001;HTML &#x4EE3;&#x7801;&#xFF0C;&#x538B;&#x7F29;&#x5408;&#x5E76;&#x56FE;&#x7247;&#x7B49;&#x3002;</li>
<li>&#x4EE3;&#x7801;&#x5206;&#x5272;&#xFF1A;&#x63D0;&#x53D6;&#x591A;&#x4E2A;&#x9875;&#x9762;&#x7684;&#x516C;&#x5171;&#x4EE3;&#x7801;&#x3001;&#x63D0;&#x53D6;&#x9996;&#x5C4F;&#x4E0D;&#x9700;&#x8981;&#x6267;&#x884C;&#x90E8;&#x5206;&#x7684;&#x4EE3;&#x7801;&#x8BA9;&#x5176;&#x5F02;&#x6B65;&#x52A0;&#x8F7D;&#x3002;</li>
<li>&#x6A21;&#x5757;&#x5408;&#x5E76;&#xFF1A;&#x5728;&#x91C7;&#x7528;&#x6A21;&#x5757;&#x5316;&#x7684;&#x9879;&#x76EE;&#x91CC;&#x4F1A;&#x6709;&#x5F88;&#x591A;&#x4E2A;&#x6A21;&#x5757;&#x548C;&#x6587;&#x4EF6;&#xFF0C;&#x9700;&#x8981;&#x6784;&#x5EFA;&#x529F;&#x80FD;&#x628A;&#x6A21;&#x5757;&#x5206;&#x7C7B;&#x5408;&#x5E76;&#x6210;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x3002;</li>
<li>&#x81EA;&#x52A8;&#x5237;&#x65B0;&#xFF1A;&#x76D1;&#x542C;&#x672C;&#x5730;&#x6E90;&#x4EE3;&#x7801;&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x81EA;&#x52A8;&#x91CD;&#x65B0;&#x6784;&#x5EFA;&#x3001;&#x5237;&#x65B0;&#x6D4F;&#x89C8;&#x5668;&#x3002;</li>
<li>&#x4EE3;&#x7801;&#x6821;&#x9A8C;&#xFF1A;&#x5728;&#x4EE3;&#x7801;&#x88AB;&#x63D0;&#x4EA4;&#x5230;&#x4ED3;&#x5E93;&#x524D;&#x9700;&#x8981;&#x6821;&#x9A8C;&#x4EE3;&#x7801;&#x662F;&#x5426;&#x7B26;&#x5408;&#x89C4;&#x8303;&#xFF0C;&#x4EE5;&#x53CA;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x662F;&#x5426;&#x901A;&#x8FC7;&#x3002;</li>
<li>&#x81EA;&#x52A8;&#x53D1;&#x5E03;&#xFF1A;&#x66F4;&#x65B0;&#x5B8C;&#x4EE3;&#x7801;&#x540E;&#xFF0C;&#x81EA;&#x52A8;&#x6784;&#x5EFA;&#x51FA;&#x7EBF;&#x4E0A;&#x53D1;&#x5E03;&#x4EE3;&#x7801;&#x5E76;&#x4F20;&#x8F93;&#x7ED9;&#x53D1;&#x5E03;&#x7CFB;&#x7EDF;&#x3002;</li>
</ul>
<h2 id="t103. Webpack">3. Webpack <a href="#t103. Webpack"> # </a></h2>
<p>Webpack &#x662F;&#x4E00;&#x4E2A;&#x6253;&#x5305;&#x6A21;&#x5757;&#x5316; JavaScript &#x7684;&#x5DE5;&#x5177;&#xFF0C;&#x5728; Webpack &#x91CC;&#x4E00;&#x5207;&#x6587;&#x4EF6;&#x7686;&#x6A21;&#x5757;&#xFF0C;&#x901A;&#x8FC7; Loader &#x8F6C;&#x6362;&#x6587;&#x4EF6;&#xFF0C;&#x901A;&#x8FC7; Plugin &#x6CE8;&#x5165;&#x94A9;&#x5B50;&#xFF0C;&#x6700;&#x540E;&#x8F93;&#x51FA;&#x7531;&#x591A;&#x4E2A;&#x6A21;&#x5757;&#x7EC4;&#x5408;&#x6210;&#x7684;&#x6587;&#x4EF6;&#x3002;Webpack &#x4E13;&#x6CE8;&#x4E8E;&#x6784;&#x5EFA;&#x6A21;&#x5757;&#x5316;&#x9879;&#x76EE;&#x3002;</p>
<p>&#x4E00;&#x5207;&#x6587;&#x4EF6;&#xFF1A;JavaScript&#x3001;CSS&#x3001;SCSS&#x3001;&#x56FE;&#x7247;&#x3001;&#x6A21;&#x677F;&#xFF0C;&#x5728; Webpack &#x773C;&#x4E2D;&#x90FD;&#x662F;&#x4E00;&#x4E2A;&#x4E2A;&#x6A21;&#x5757;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x597D;&#x5904;&#x662F;&#x80FD;&#x6E05;&#x6670;&#x7684;&#x63CF;&#x8FF0;&#x51FA;&#x5404;&#x4E2A;&#x6A21;&#x5757;&#x4E4B;&#x95F4;&#x7684;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#xFF0C;&#x4EE5;&#x65B9;&#x4FBF; Webpack &#x5BF9;&#x6A21;&#x5757;&#x8FDB;&#x884C;&#x7EC4;&#x5408;&#x548C;&#x6253;&#x5305;&#x3002; &#x7ECF;&#x8FC7; Webpack &#x7684;&#x5904;&#x7406;&#xFF0C;&#x6700;&#x7EC8;&#x4F1A;&#x8F93;&#x51FA;&#x6D4F;&#x89C8;&#x5668;&#x80FD;&#x4F7F;&#x7528;&#x7684;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x3002;</p>
<h2 id="t113.1 &#x5B89;&#x88C5; Webpack">3.1 &#x5B89;&#x88C5; Webpack <a href="#t113.1 &#x5B89;&#x88C5; Webpack"> # </a></h2>
<p>&#x5728;&#x7528; Webpack &#x6267;&#x884C;&#x6784;&#x5EFA;&#x4EFB;&#x52A1;&#x65F6;&#x9700;&#x8981;&#x901A;&#x8FC7; webpack &#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x53BB;&#x542F;&#x52A8;&#x6784;&#x5EFA;&#x4EFB;&#x52A1;&#xFF0C;&#x6240;&#x4EE5;&#x9700;&#x8981;&#x5B89;&#x88C5; webpack &#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;</p>
<h2 id="t123.1.1  &#x5B89;&#x88C5; Webpack &#x5230;&#x672C;&#x9879;&#x76EE;">3.1.1  &#x5B89;&#x88C5; Webpack &#x5230;&#x672C;&#x9879;&#x76EE; <a href="#t123.1.1  &#x5B89;&#x88C5; Webpack &#x5230;&#x672C;&#x9879;&#x76EE;"> # </a></h2>
<pre><code class="lang-javascript"># &#x5B89;&#x88C5;&#x6700;&#x65B0;&#x7A33;&#x5B9A;&#x7248;
npm i -D webpack

# &#x5B89;&#x88C5;&#x6307;&#x5B9A;&#x7248;&#x672C;
npm i -D <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="354250574554565e75">[email&#160;protected]</a>&lt;version&gt;

# &#x5B89;&#x88C5;&#x6700;&#x65B0;&#x4F53;&#x9A8C;&#x7248;&#x672C;
npm i -D <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed9a888f9d8c8e86ad8f88998c">[email&#160;protected]</a>
</code></pre>
<blockquote>
<p>npm i -D &#x662F; <code>npm install --save-dev</code> &#x7684;&#x7B80;&#x5199;&#xFF0C;&#x662F;&#x6307;&#x5B89;&#x88C5;&#x6A21;&#x5757;&#x5E76;&#x4FDD;&#x5B58;&#x5230; <code>package.json</code> &#x7684; <code>devDependencies</code></p>
</blockquote>
<h2 id="t133.1.2  &#x5B89;&#x88C5; Webpack &#x5230;&#x5168;&#x5C40;">3.1.2  &#x5B89;&#x88C5; Webpack &#x5230;&#x5168;&#x5C40; <a href="#t133.1.2  &#x5B89;&#x88C5; Webpack &#x5230;&#x5168;&#x5C40;"> # </a></h2>
<p>&#x5B89;&#x88C5;&#x5230;&#x5168;&#x5C40;&#x540E;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x4F55;&#x5730;&#x65B9;&#x5171;&#x7528;&#x4E00;&#x4E2A; Webpack &#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#xFF0C;&#x800C;&#x4E0D;&#x7528;&#x5404;&#x4E2A;&#x9879;&#x76EE;&#x91CD;&#x590D;&#x5B89;&#x88C5;</p>
<pre><code class="lang-javascript">npm i -g webpack
</code></pre>
<blockquote>
<p>&#x63A8;&#x8350;&#x5B89;&#x88C5;&#x5230;&#x5F53;&#x524D;&#x9879;&#x76EE;&#xFF0C;&#x539F;&#x56E0;&#x662F;&#x53EF;&#x9632;&#x6B62;&#x4E0D;&#x540C;&#x9879;&#x76EE;&#x4F9D;&#x8D56;&#x4E0D;&#x540C;&#x7248;&#x672C;&#x7684; Webpack &#x800C;&#x5BFC;&#x81F4;&#x51B2;&#x7A81;</p>
</blockquote>
<h2 id="t143.1.2 &#x4F7F;&#x7528; Webpack">3.1.2 &#x4F7F;&#x7528; Webpack <a href="#t143.1.2 &#x4F7F;&#x7528; Webpack"> # </a></h2>
<h2 id="t153.1.3 &#x4F7F;&#x7528; Webpack">3.1.3 &#x4F7F;&#x7528; Webpack <a href="#t153.1.3 &#x4F7F;&#x7528; Webpack"> # </a></h2>
<pre><code class="lang-javascript">(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">modules</span>) </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">require</span>(<span class="hljs-params">moduleId</span>) </span>{
        <span class="hljs-keyword">var</span> <span class="hljs-built_in">module</span> = {
            <span class="hljs-attr">exports</span>: {}
        };
        modules[moduleId].call(<span class="hljs-built_in">module</span>.exports, <span class="hljs-built_in">module</span>, <span class="hljs-built_in">module</span>.exports, <span class="hljs-built_in">require</span>);
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">module</span>.exports;

    }
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;./index.js&quot;</span>);
})
    ({
        <span class="hljs-string">&quot;./index.js&quot;</span>:
            (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">module, exports, require</span>) </span>{
                <span class="hljs-built_in">eval</span>(<span class="hljs-string">&quot;console.log(&apos;hello&apos;);\n\n&quot;</span>);
            })
    });
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-meta">#! /usr/bin/env node</span>
<span class="hljs-keyword">const</span> pathLib = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> ejs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;ejs&apos;</span>);
<span class="hljs-keyword">let</span> cwd = process.cwd();
<span class="hljs-keyword">let</span> { entry, <span class="hljs-attr">output</span>: { filename, path } } = <span class="hljs-built_in">require</span>(pathLib.join(cwd, <span class="hljs-string">&apos;./webpack.config.js&apos;</span>));
<span class="hljs-keyword">let</span> script = fs.readFileSync(entry, <span class="hljs-string">&apos;utf8&apos;</span>);
<span class="hljs-keyword">let</span> bundle = <span class="hljs-string">`
(function (modules) {
    function require(moduleId) {
        var module = {
            exports: {}
        };
        modules[moduleId].call(module.exports, module, module.exports, require);
        return module.exports;

    }
    return require(&quot;&lt;%-entry%&gt;&quot;);
})
    ({
        &quot;&lt;%-entry%&gt;&quot;:
            (function (module, exports, require) {
                eval(&quot;&lt;%-script%&gt;&quot;);
            })
    });
`</span>
<span class="hljs-keyword">let</span> bundlejs = ejs.render(bundle, {
    entry,
    script
});
<span class="hljs-keyword">try</span> {
    fs.writeFileSync(pathLib.join(path, filename), bundlejs);
} <span class="hljs-keyword">catch</span> (e) {
    <span class="hljs-built_in">console</span>.error(<span class="hljs-string">&apos;&#x7F16;&#x8BD1;&#x5931;&#x8D25; &apos;</span>, e);
}
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;compile sucessfully!&apos;</span>);
</code></pre>
<h2 id="t163.1.4 &#x4F9D;&#x8D56;&#x5176;&#x5B83;&#x6A21;&#x5757;">3.1.4 &#x4F9D;&#x8D56;&#x5176;&#x5B83;&#x6A21;&#x5757; <a href="#t163.1.4 &#x4F9D;&#x8D56;&#x5176;&#x5B83;&#x6A21;&#x5757;"> # </a></h2>
<pre><code class="lang-javascript"><span class="hljs-meta">#! /usr/bin/env node</span>
<span class="hljs-keyword">const</span> pathLib = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">let</span> ejs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;ejs&apos;</span>);
<span class="hljs-keyword">let</span> cwd = process.cwd();
<span class="hljs-keyword">let</span> { entry, <span class="hljs-attr">output</span>: { filename, path } } = <span class="hljs-built_in">require</span>(pathLib.join(cwd, <span class="hljs-string">&apos;./webpack.config.js&apos;</span>));
<span class="hljs-keyword">let</span> script = fs.readFileSync(entry, <span class="hljs-string">&apos;utf8&apos;</span>);
<span class="hljs-keyword">let</span> modules = [];
script.replace(<span class="hljs-regexp">/require\([&apos;&quot;](.+?)[&apos;&quot;]\)/g</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> name = <span class="hljs-built_in">arguments</span>[<span class="hljs-number">1</span>];
    <span class="hljs-keyword">let</span> script = fs.readFileSync(name, <span class="hljs-string">&apos;utf8&apos;</span>);
    modules.push({
        name,
        script
    });
});
<span class="hljs-keyword">let</span> bundle = <span class="hljs-string">`
(function (modules) {
    function require(moduleId) {
        var module = {
            exports: {}
        };
        modules[moduleId].call(module.exports, module, module.exports, require);
        return module.exports;
    }
    return require(&quot;&lt;%-entry%&gt;&quot;);
})
    ({
        &quot;&lt;%-entry%&gt;&quot;:
            (function (module, exports, require) {
                eval(\`&lt;%-script%&gt;\`);
            })
       &lt;%if(modules.length&gt;0){%&gt;,&lt;%}%&gt;
        &lt;%for(let i=0;i&lt;modules.length;i++){
            let module = modules[i];%&gt;   
            &quot;&lt;%-module.name%&gt;&quot;:
            (function (module, exports, require) {
                eval(\`&lt;%-module.script%&gt;\`);
            })
        &lt;% }%&gt;    
    });
`</span>
<span class="hljs-keyword">let</span> bundlejs = ejs.render(bundle, {
    entry,
    script,
    modules
});
<span class="hljs-keyword">try</span> {
    fs.writeFileSync(pathLib.join(path, filename), bundlejs);
} <span class="hljs-keyword">catch</span> (e) {
    <span class="hljs-built_in">console</span>.error(<span class="hljs-string">&apos;&#x7F16;&#x8BD1;&#x5931;&#x8D25; &apos;</span>, e);
}
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;compile sucessfully!&apos;</span>);




</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script data-cfasync="false" src="/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script><script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
